/*
 * read notes at the end of file
 * */
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace PocketLibrary.Management
{
    public class RememberTheMilk : IService
    {
        #region IService Members

        public ServiceType ServiceType
        {
            get { return ServiceType.Management; }
        }

        #endregion

        #region private variables
        private string _frob = "";
        private string _restUrl = "http://www.rememberthemilk.com/services/rest/";
        private string _sslRestUrl = "https://www.rememberthemilk.com/services/rest/";
        private string _authUrl = "http://www.rememberthemilk.com/services/auth/";


        #endregion

        #region fields
        private string _apiKey;

        public string ApiKey
        {
            get { return _apiKey; }
            set { _apiKey = value; }
        }

        private bool _useSsl=false;

        public bool UseSSSL
        {
            get { return _useSsl; }
            set { _useSsl = value; }
        }
        
        #endregion

        #region helper methods

        private string ByteArrayToHexString(byte[] data)
        {
            StringBuilder sb = new StringBuilder(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            return sb.ToString().ToLower();
        }

        private string SignRequest(string request)
        {
            //MD5 mymd5 = MD5.Create();
            //byte[] hash = mymd5.ComputeHash(Encoding.ASCII.GetBytes(request));
            //return ByteArrayToHexString(hash);
            System.Security.Cryptography.MD5CryptoServiceProvider csp = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(request);
            byte[] hashedBytes = csp.ComputeHash(bytes, 0, bytes.Length);
            return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
        }

        private HttpWebRequest CreateRequest(string uri)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            //request.PreAuthenticate = true;
            request.ProtocolVersion = new Version("1.1");
            //request.SendChunked = false;
            request.UserAgent = "PocketApps 0.2/RTM";
            request.Method = "GET";
            //request.Credentials = new NetworkCredential(UserName, PassWord);

            //byte[] _basic = Encoding.ASCII.GetBytes(UserName + ":" + PassWord);
            //string _strbasic = Convert.ToBase64String(_basic);
            //request.Headers.Add("Authorization: Basic " + _strbasic);

            return request;
        }

        private string ReadResponseBody(HttpWebResponse response)
        {
            string body;

            Stream stream = response.GetResponseStream();

            StreamReader reader = new StreamReader(stream, Encoding.UTF8);

            body = reader.ReadToEnd();

            stream.Close();

            return body;
        }

        private void WriteRequestBody(HttpWebRequest request, string body)
        {
            byte[] bytes;

            bytes = Encoding.UTF8.GetBytes(body);

            request.ContentLength = bytes.Length;

            Stream stream = request.GetRequestStream();

            stream.Write(bytes, 0, bytes.Length);

            stream.Close();
        }
        #endregion

        public bool Login()
        {

            //Dictionary<string, string> requestParams = new Dictionary<string, string>();
            //requestParams.Add(
            return false;

        }

        private string rtmAuthGetFrob(string ApiKey)
        {
            return "not implemented";
        }
    }
}

/*
 * available methods as of 8.11.2007
 * user authentication
 * http://www.rememberthemilk.com/services/api/authentication.rtm
 * rest requests
 * http://www.rememberthemilk.com/services/api/request.rest.rtm
 * available methods
 * http://www.rememberthemilk.com/services/api/methods/
 * 
auth

    * rtm.auth.checkToken
    * rtm.auth.getFrob
    * rtm.auth.getToken

contacts

    * rtm.contacts.add
    * rtm.contacts.delete
    * rtm.contacts.getList

groups

    * rtm.groups.add
    * rtm.groups.addContact
    * rtm.groups.delete
    * rtm.groups.getList
    * rtm.groups.removeContact

lists

    * rtm.lists.add
    * rtm.lists.archive
    * rtm.lists.delete
    * rtm.lists.getList
    * rtm.lists.setDefaultList
    * rtm.lists.setName
    * rtm.lists.unarchive

locations

    * rtm.locations.getList

reflection

    * rtm.reflection.getMethodInfo
    * rtm.reflection.getMethods

settings

    * rtm.settings.getList

tasks

    * rtm.tasks.add
    * rtm.tasks.addTags
    * rtm.tasks.complete
    * rtm.tasks.delete
    * rtm.tasks.getList
    * rtm.tasks.movePriority
    * rtm.tasks.moveTo
    * rtm.tasks.postpone
    * rtm.tasks.removeTags
    * rtm.tasks.setDueDate
    * rtm.tasks.setEstimate
    * rtm.tasks.setLocation
    * rtm.tasks.setName
    * rtm.tasks.setPriority
    * rtm.tasks.setRecurrence
    * rtm.tasks.setTags
    * rtm.tasks.setURL
    * rtm.tasks.uncomplete

tasks.notes

    * rtm.tasks.notes.add
    * rtm.tasks.notes.delete
    * rtm.tasks.notes.edit

test

    * rtm.test.echo
    * rtm.test.login

time

    * rtm.time.convert
    * rtm.time.parse

timelines

    * rtm.timelines.create

timezones

    * rtm.timezones.getList

transactions

    * rtm.transactions.undo


 * */
